<?php

namespace App\Http\Controllers\Admin\User;

use App\Http\Controllers\Controller;
use App\Models\UserLabel;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\User\UserLabelsTransformer;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class UserLableController extends Controller
{
    use AdminTraits;

    public $model = UserLabel::class;

    public $transformer = UserLabelsTransformer::class;

    /**
     * 进行表单验证
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [
                    'label_category_id' => 'required',
                    'label_name' => ['required', 'unique:user_labels'],
                ];
                break;
            case 'PATCH':
            case 'PUT':
                return [
                    'label_name' => [Rule::unique('user_labels', 'label_name')
                        // ignore() 方法去除当前的id字段
                        ->ignore(route_parameter('lable'), 'id')],
                ];
                break;
        }
        return [];
    }

    /**
     * 表单验证错误时的提示
     */
    public function ruleMessages(): array
    {
        return [
            'label_category_id.required' => '请选择标签分类',
            'label_name.required' => '请填写标签名称',
            'label_name.unique' => '该标签已经存在,请重新添加',
        ];
    }
}
